package is202.hrms.ejb;


import is202.hrms.entity.Modulen;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;



/**
 * This ejb handles database operations for Modulen objects.
 * It does not have any fields (except for the entity manager which
 * is handled by the container), which means it can't keep state, and
 * is annotated with Stateless to mark it as a stateless session bean.
 * A stateless bean can be shared by several modulens, so it is efficient
 * with regards to resource usage.
 *
 * @author evenal
 */
@Stateless
public class ModulenEJB extends AbstractFacade<Modulen> {
    /**
     * The PersistenceContext annotation is a request to  the
     * container (GlassFish) to create or find an EntityManager and
     * store it in the field em. We get the EntityManager object we need,
     * without having to write the code to create it. This is called
     * modulenendency injection.
     */
    @PersistenceContext
    private EntityManager em;

    /** This method is used in the methods inherited from AbstractFacade
     * to access the EntityManager */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ModulenEJB() {
        super(Modulen.class);
    }

    /** Method returns the first modulenartment it finds. It is used by the
     * employee page, to make sure that every employee belongs to a modulenartment
     */
    public Modulen getDefaultModulen() {
        List<Modulen> modulenList = findAll();
        if (modulenList != null && modulenList.size() > 0) {
            return modulenList.get(0);
        } else {
            throw new IllegalStateException("Ingen modulenr eksiterer.");
        }
    }

    /** A wrapper for the find() method in AbstractFacade. It allows us
     * to pass the primary key as the primitive type long, rather than
     * as the wrapper type Long. It also forces loading of the modulenartment's
     * employees */
    public Modulen find(int modulenId) {
        Modulen modulen = super.find(modulenId);
        return modulen;
    }

    @Override
    public void delete(Modulen modulen) {
        modulen = em.merge(modulen);
        super.delete(modulen);
        
    }

}
